home *** CD-ROM | disk | FTP | other *** search
- /*+
- * File: which.c
- * Description:
- * Tries to mimic Unix's which as closely as possible.
- *
- * Usages:
- * which <i_files>
- *
- *
- * Author: Mohsen Banan.
- *
- * This program is public domain software, no warranty intended or
- * implied.
- * General permission to copy or modify, but not for profit, is
- * hereby granted.
- *
- * Functions:
- *
- *
- * Audit Trail: $Log: which.c,v $
- * Revision 1.1 85/08/28 08:38:54 mohsen
- * original posting to the net
- *
- *
- *
- -*/
-
- #ifdef RCS_VER
- static char *rcs = "$Header: which.c,v 1.1 85/08/28 08:38:54 mohsen Exp $";
- #endif
-
- /* #includes */
- #include "mbstd.h"
- #include <stdio.h>
-
- /* #defines */
- #ifdef BSD4_2
- #define strchr index
- #endif
-
- /* external variables */
-
- /* referenced external function declarations */
- EXTERN char *getenv();
- EXTERN char *strchr();
- EXTERN char *strcat();
-
- /* internal function declarations */
- PUBLIC VOID which();
- STATIC CHAR * get_head();
- STATIC BOOL is_runable();
- STATIC VOID usage();
-
- /* global variables */
-
- /* static variables */
- STATIC CHAR * prog_name;
-
- INT
- main (argc, argv)
- INT argc;
- CHAR * argv[];
- {
- which(argc, argv);
- }
-
-
- /*<
- * Function:which
- * Description:
- *
- * Returns:
- * VOID
- *
- >*/
- PUBLIC VOID
- which (argc,argv)
- INT argc;
- CHAR * argv[];
- {
-
- INT i;
- CHAR * head, * nxt_head; /* head concept is a-la Csh */
- BOOL end_of_path;
- BOOL not_in_path;
- STATIC CHAR file_buf[256];
- STATIC CHAR path_buf[512];
- CHAR * orig_path;
-
- prog_name = argv[0];
- if (argc < 2) {
- usage();
- exit(1);
- }
- if ((orig_path = getenv("PATH")) == NULL) {
- orig_path = ".";
- }
- for (i=1; i<argc; ++i) {
- end_of_path = FALSE;
- not_in_path = TRUE;
- strcpy (path_buf, orig_path);
- nxt_head = head = path_buf;
- while ( ! end_of_path ) {
- nxt_head = get_head (head);
- if ( nxt_head == NULL) {
- end_of_path = TRUE;
- } else {
- *nxt_head = '\0';
- }
- #ifdef unix
- sprintf (file_buf, "%s/%s",(*head ? head:"."), argv[i]);
- #endif
- #ifdef MSDOS
- sprintf (file_buf, "%s\\%s",(*head ? head:"."), argv[i]);
- #endif
- head = ++nxt_head;
- if ( is_runable(file_buf) ) {
- printf ("%s\n", file_buf);
- not_in_path = FALSE;
- }
- }
- if ( not_in_path ) {
- printf ("No %s in %s\n", argv[i], orig_path);
- }
- } /* for i<argc */
- exit (0);
- }
-
- STATIC VOID
- usage ()
- {
- fprintf (stderr, "Usage: %s cmd [cmd, ..]\n", prog_name);
- }
-
-
- STATIC CHAR *
- get_head (head)
- CHAR * head;
- {
- #ifdef unix
- return (strchr( head, ':'));
- #endif
- #ifdef MSDOS
- return (strchr( head, ';'));
- #endif
- }
-
- STATIC BOOL
- is_runable (file_buf)
- CHAR * file_buf;
- {
- BOOL ret_val;
- CHAR * name, * root_name; /* root is as defined by Csh */
-
- ret_val = FALSE;
- #ifdef unix
- if ( access(file_buf, 1) == 0 ) {
- ret_val = TRUE;
- }
- #endif
- #ifdef MSDOS
- for (root_name=file_buf; *root_name; ++root_name);
- name = strcat(file_buf, ".com");
- if ( access(name, 0) == 0 ) {
- ret_val = TRUE;
- }
- else {
- *root_name = '\0';
- name = strcat(file_buf, ".exe");
- if ( access(name, 0) == 0 ) {
- ret_val = TRUE;
- }
- else {
- *root_name = '\0';
- name = strcat(file_buf, ".bat");
- if ( access(name, 0) == 0 ) {
- ret_val = TRUE;
- }
- }
- }
- printf ("\n %s \n", name);
- #endif
- return (ret_val);
- }
-